package base.day05;

/**
 * 描述：
 *      给定一个节点，找到这个节点的后继节点
 *      后续节点：一个二叉树按照中序遍历的顺序中，节点之间的相对位置是就是前驱、后继节点的定义
 * @author hl
 * @version 1.0
 * @date 2020/10/27 15:39
 */
public class Code_03_SuccessorNode {

    public static Node getSuccessorNode(Node node){
        if (node == null) {
            return node;
        }
        if(node.right != null){//如果有右子树，后继节点就是右子树的最左节点
            return getLeftMost(node.right);
        }else{//如果没有右子树，那么后继节点就是第一个父节点作为左孩子的节点的父节点，如果没有就返回null
            Node parent = node.parent;
            Node cur = node;
            while(parent != null && parent.left != cur){
                cur = parent;
                parent = cur.parent;
            }
            return parent;
        }
    }
    private static Node getLeftMost(Node node) {
        if (node == null) {
            return node;
        }
        Node cur = node;
        while(cur.left != null){
            cur = cur.left;
        }
        return cur;
    }
    public static Node getPreNode(Node node){
        if (node == null) {
            return node;
        }
        if (node.left != null) {//如果左子树不为空，那么前驱节点就是左子树的最右节点
            return getRightMost(node.left);
        }else{//如果左子树为空，那么前驱节点就是父节点中第一个作为右孩子节点的父节点，如果没有就返回null
            Node parent = node.parent;
            Node cur = node;
            while (parent != null && parent.right != cur) {
                cur = parent;
                parent = cur.parent;
            }
            return parent;
        }
    }

    private static Node getRightMost(Node node) {
        if (node == null) {
            return node;
        }
        Node cur = node;
        while(node.right != null){
            node = node.right;
        }
        return cur;
    }

    public static void main(String[] args) {
        Node head = new Node(6);
        head.parent = null;
        head.left = new Node(3);
        head.left.parent = head;
        head.left.left = new Node(1);
        head.left.left.parent = head.left;
        head.left.left.right = new Node(2);
        head.left.left.right.parent = head.left.left;
        head.left.right = new Node(4);
        head.left.right.parent = head.left;
        head.left.right.right = new Node(5);
        head.left.right.right.parent = head.left.right;
        head.right = new Node(9);
        head.right.parent = head;
        head.right.left = new Node(8);
        head.right.left.parent = head.right;
        head.right.left.left = new Node(7);
        head.right.left.left.parent = head.right.left;
        head.right.right = new Node(10);
        head.right.right.parent = head.right;

        Code_02_PrintBinaryTree.printTree(head);

//        Node test = head.left.left;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.left.left.right;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.left;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.left.right;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.left.right.right;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.right.left.left;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.right.left;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.right;
//        System.out.println(test.value + " next: " + getSuccessorNode(test).value);
//        test = head.right.right; // 10's next is null
//        System.out.println(test.value + " next: " + getSuccessorNode(test));

        Node test = head.left.left;
        System.out.println(test.value + " next: " + getPreNode(test));
        test = head.left.left.right;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.left;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.left.right;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.left.right.right;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.right.left.left;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.right.left;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.right;
        System.out.println(test.value + " next: " + getPreNode(test).value);
        test = head.right.right; // 10's next is null
        System.out.println(test.value + " next: " + getPreNode(test).value);
    }
}
